package view.backend.product;

import exceptionManager.ExceptionManager;
import java.awt.Cursor;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;
import javax.swing.*;
import model.Product;
import model.ProgramValue;
import utility.Converter;
import view.generaldesign.DesignLayout;

/**
 *
 * @author Sonny Sandberg
 */
public class AdminProductListPanel extends JPanel
{
    private AdminProduct adminProduct;

    public AdminProductListPanel(AdminProduct view, final Product product)
    {
        this.adminProduct = view;
        ProgramValue programValues = null;
        try
        {
            programValues = model.handlers.ProgramValueHandler.getCurrentProgramValueModelObject();
        } 
        catch (SQLException ex)
        {
            ExceptionManager.handleSQLException(ex);
        } 
        catch (ClassNotFoundException ex)
        {
            ExceptionManager.handleClassNotFoundException(ex);
        }
        
        setLayout(null);
        setBounds(0, 0, 500, 180);
        setBackground(DesignLayout.T_FIELD_BG_COLOR);
        setBorder(DesignLayout.T_FIELD_RED);
        
        JLabel lblProductIDAndName = new JLabel("<html><b>"+product.getId()+"</b> "+product.getName()+"</html>");
        lblProductIDAndName.setBounds(10, 10, 520, 20);
        lblProductIDAndName.setFont(DesignLayout.STANDARD);
        
        JLabel lblProductFamily = new JLabel("<html><b>Varegruppe:</b> "+product.getProductFamily().getId()+" "+product.getProductFamily().getName()+"</html>");
        lblProductFamily.setBounds(10, 30, 520, 20);
        lblProductFamily.setFont(DesignLayout.STANDARD);
        
        JLabel lblPurchase = new JLabel("<html>Købspris: "+product.getPurchasePrice()+" ("+ (product.getPurchasePrice() * (1 + (programValues.getVat() / 100))) +" inklusiv moms)</html>");
        lblPurchase.setBounds(10, 50, 520, 20);
        lblPurchase.setFont(DesignLayout.STANDARD);
        
        JLabel lblSale = new JLabel("<html>Salgspris: "+product.getSalesPrice()+" ("+ (product.getSalesPrice()* (1 + (programValues.getVat() / 100))) +" inklusiv moms)</html>");
        lblSale.setBounds(10, 70, 520, 20);
        lblSale.setFont(DesignLayout.STANDARD);
        
        JLabel lblSize = new JLabel("<html><b>Størrelse:</b> "+product.getSize()+"</html>");
        lblSize.setBounds(10, 90, 520, 20);
        lblSize.setFont(DesignLayout.STANDARD);
        
        JLabel lblDecorated = new JLabel("<html><b>Har Dekoration: </b> "+Converter.booleanToString(product.isHasDecoration())+"</html>");
        lblDecorated.setBounds(10, 110, 520, 20);
        lblDecorated.setFont(DesignLayout.STANDARD);
        
        JLabel lblServiceProduct = new JLabel("<html><b>Serviceprodukt (multisalg)</b> "+Converter.booleanToString(product.isRemainAfterSale())+"</html>");
        lblServiceProduct.setBounds(10, 130, 520, 20);
        lblServiceProduct.setFont(DesignLayout.STANDARD);
        
        JLabel lblActive = new JLabel("<html><b>Produkt Aktivt: </b> "+Converter.booleanToString(product.isActive())+"</html>");
        lblActive.setBounds(10, 150, 520, 20);
        lblActive.setFont(DesignLayout.STANDARD);
        
        add(lblProductIDAndName);
        add(lblProductFamily);
        add(lblPurchase);
        add(lblSale);
        add(lblSize);
        add(lblDecorated);
        add(lblServiceProduct);
        add(lblActive);
        
        MouseAdapter mouseReleased;
        mouseReleased =
                (
                    new MouseAdapter()
                    {
                        @Override
                        public void mouseReleased(MouseEvent evt)
                        {
                            if (model.BSSingleton.getSingletonObject().getLoggedInUser().getAccessLevel() > 1)
                            {
                                adminProduct.setProductModel(product);
                                adminProduct.getAdminNewProduct().setFieldsFromModel();
                                adminProduct.getPrdList().getPanel().setVisible(false);
                                adminProduct.getPrdSearch().getPanel().setVisible(false);                            
                                adminProduct.getAdminNewProduct().getPanel().setVisible(true);
                                adminProduct.getAdminNewProductOverview().updateLabelsFromModel();
                                adminProduct.getAdminNewProductOverview().getPanel().setVisible(true);
                                adminProduct.setBtnNewVisible(false);
                                adminProduct.setBtnGoToVisible(true);
                            }                            
                        }
                    }
                );
        
        MouseAdapter 
                    mouseEntered,
                    mouseExited;
        
        mouseEntered =
                (
                    new MouseAdapter()
                    {
                        @Override
                        public void mouseEntered(MouseEvent evt)
                        {
                            setCursor(new Cursor(Cursor.HAND_CURSOR));
                        }
                    }
                );
        
        mouseExited =
                (
                    new MouseAdapter()
                    {
                        @Override
                        public void mouseExited(MouseEvent evt)
                        {
                            setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
                        }
                    }
                );
        
        this.addMouseListener(mouseEntered);
        this.addMouseListener(mouseExited);
        this.addMouseListener(mouseReleased);
    }
}